home *** CD-ROM | disk | FTP | other *** search
/ Night Owl 6 / Night Owl's Shareware - PDSI-006 - Night Owl Corp (1990).iso / 025a / crdepp.zip / TEXPORT.C < prev    next >
C/C++ Source or Header  |  1991-01-13  |  3KB  |  120 lines

  1. /* texport.c */
  2.  
  3. /***********************************************************************/
  4. /*                                                                     */
  5. /* Command-line program which will export a CRDE table to either ascii */
  6. /* dBASE III Plus formats.                                             */
  7. /*                                          */
  8. /* Usage: texport [options] <tablename>                                */
  9. /*                                                                     */
  10. /* To compile this program requires the creation of a project file     */
  11. /* which should look something like                                    */
  12. /*                                                                     */
  13. /*   texport                                                           */
  14. /*   crde.lib                                                          */
  15. /*                                                                     */
  16. /***********************************************************************/
  17.  
  18.  
  19. #include <dir.h>
  20. #include <stdio.h>
  21. #include <string.h>
  22. #include "crde.h"
  23.  
  24. /* output formats */
  25.  
  26. #define DEFAULT 0
  27. #define ASCII    1
  28. #define dBASE      2
  29.  
  30. /* main program */
  31.  
  32. int tbuffers = 128;
  33.  
  34. int main(int argc, char *argv[])
  35. {
  36.   TABLE *t;
  37.   char path[MAXPATH], drive[MAXDRIVE], dir[MAXDIR], file[MAXFILE],
  38.     ext[MAXEXT];
  39.   int n, flags, type;
  40.   long result;
  41.  
  42.   /* print help screen if no parameters */
  43.   if (argc < 2) {
  44.     printf("\nusage: texport [options] <CRDE tablename> <destination>\n");
  45.     printf("command-line options:\n");
  46.     printf("\t-a\toutput ascii file (default)\n");
  47.     printf("\t-d\toutput dBASE file (default if '.DBF' extension)\n");
  48.     printf("\n\n");
  49.     exit(1);
  50.   }
  51.  
  52.   /* get command-line options */
  53.   type = DEFAULT;
  54.   for (n = 1; n < argc && *argv[n] == '-'; n++) {
  55.     switch (*(argv[n]+1)) {
  56.       case 'a' : type = ASCII; break;
  57.       case 'd' : type = dBASE; break;
  58.       default  :
  59.     printf("-%c is not a valid command line option.", *(argv[n]+1));
  60.     exit(1);
  61.     }
  62.   }
  63.  
  64.   /* no source file */
  65.   if (n == argc) {
  66.     printf("You did not specify a table to export.\n");
  67.     exit(1);
  68.   }
  69.  
  70.   /* get destination file */
  71.   if (n == argc - 1)
  72.     strcpy(path, argv[n]);
  73.   else
  74.     strcpy(path, argv[n+1]);
  75.  
  76.   /* determine default type */
  77.   flags = fnsplit(path, drive, dir, file, ext);
  78.   if (type == DEFAULT)
  79.     if ((flags & EXTENSION) && (strcpy(strupr(ext), "DBF")))
  80.       type = dBASE;
  81.     else
  82.       type = ASCII;
  83.  
  84.   /* default extension */
  85.   if (n == argc - 1)
  86.     switch (type) {
  87.       case ASCII : strcat(path, ".TXT"); break;
  88.       case dBASE : strcat(path, ".DBF"); break;
  89.     }
  90.  
  91.   /* open table */
  92.   if ((t = topen(argv[n])) == NULL) {
  93.     printf("Unable to open table (error code = %d).\n", terrno);
  94.     exit(1);
  95.   }
  96.  
  97.   /* export table */
  98.   switch (type) {
  99.     case ASCII : result = texportascii(t, path); break;
  100.     case dBASE : result = texportdBASE(t, path); break;
  101.   }
  102.  
  103.   /* check results */
  104.   if (result < 0)
  105.     printf("Export failed (error code %d occurred).\n", terrno);
  106.   else {
  107.     printf("Export to ");
  108.     switch (type) {
  109.       case ASCII : printf("ascii"); break;
  110.       case dBASE : printf("dBASE"); break;
  111.     }
  112.     printf(" file %s was successful.\n", path);
  113.     printf("%ld rows exported.\n", result);
  114.   }
  115.   tclose(t);
  116. }
  117.  
  118.  
  119. /* end of texport.c */
  120.